package org.apache.database.utils;

import org.apache.database.anno.FieldName;
import org.apache.database.anno.PrimaryKey;
import org.apache.database.anno.TabName;
import org.apache.database.excpt.ConfigError;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class AnnotationUtil {
    public static String getTableName(Class<?> s){
        TabName declaredAnnotation1 = s.getDeclaredAnnotation(TabName.class);
        return declaredAnnotation1==null?null:declaredAnnotation1.value();
    }

    /**
     * 获取所有字段对应数据库列表
     * @param s
     * @return
     */
    public static List<String> getFieldsNames(Class<?>s){
        List<String> result =new ArrayList<>();
        Field[] declaredFields = s.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            result.add(getColumnName(declaredField));
        }
        return result;
    }

    /**
     * 获取主键名称
     * @param t
     * @return
     */
    public static  String getPrimaryKey(Class<?> t){
        Field[] declaredFields = t.getDeclaredFields();

        int count=0;
        for (Field declaredField : declaredFields) {
            if (declaredField.getDeclaredAnnotation(PrimaryKey.class)!=null){
                ++count;
            }
        }
        if (count!=1){
            throw  new ConfigError(count==0?"主键不存在":"主键存在多个");
        }
        for (Field declaredField : declaredFields) {
            if (declaredField.getDeclaredAnnotation(PrimaryKey.class)!=null){
                return getColumnName(declaredField);
            }
        }
        return null;
    }
    /**
     * 推算出字段对应的数据库字段
     * @param field
     * @return
     */
    public   static  String getColumnName(Field field){
        FieldName declaredAnnotation = field.getDeclaredAnnotation(FieldName.class);
        if (declaredAnnotation!=null){
            return declaredAnnotation.value();
        }
        StringBuffer stringBuffer =new StringBuffer();

        char[] chars = field.getName().toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if ((int)chars[i]>=65 && (int)chars[i]<=90){
                stringBuffer.append("_");
                stringBuffer.append(String.valueOf(chars[i]).toLowerCase());
            }else {
                stringBuffer.append(chars[i]);
            }
        }
        return stringBuffer.toString();
    }
    public static Field getPrimaryKeyField(Object o){
        Field[] declaredFields = o.getClass().getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            if (declaredField.getDeclaredAnnotation(PrimaryKey.class)!=null){
               return declaredField;
            }
        }
        return null;
    }

    /**
     * 获取对应注解的字段值
     * @param o
     * @param an
     * @return
     * @throws IllegalAccessException
     */
    public static Serializable getValueByAnnotation(Object o, Class<? extends Annotation> an) throws IllegalAccessException {
        Field[] declaredFields = o.getClass().getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            if (declaredField.getDeclaredAnnotation(an)!=null){
                return (Serializable) declaredField.get(o);
            }
        }
        return null;
    }
}
